package com.ultrasoft.farmasoft.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Locality;
import com.ultrasoft.farmasoft.domain.Pharmacyproduct;
import com.ultrasoft.farmasoft.domain.Product;
import com.ultrasoft.farmasoft.domain.Provider;
import com.ultrasoft.farmasoft.domain.Providerpharmacy;
import com.ultrasoft.farmasoft.domain.Rolprovider;
import com.ultrasoft.farmasoft.domain.Userprovider;
import com.ultrasoft.farmasoft.repository.PharmacyProductDao;
import com.ultrasoft.farmasoft.repository.ProductDao;
import com.ultrasoft.farmasoft.repository.ProviderDao;
import com.ultrasoft.farmasoft.repository.ProviderPharmacyDao;
import com.ultrasoft.farmasoft.repository.RolProviderDao;
import com.ultrasoft.farmasoft.repository.UserProviderDao;

@Service
public class ProviderServiceImpl implements ProviderService{
	private ProviderDao providerDao;
	private RolProviderDao rolProviderDao;
	private UserProviderDao userProviderDao; 
	private ProviderPharmacyDao providerPharmacyDao;
	@Autowired
	public ProviderServiceImpl(ProviderPharmacyDao providerPharmacyDao,UserProviderDao userProviderDao,RolProviderDao rolProviderDao,ProviderDao providerDao) {
		this.providerDao 		= providerDao;
		this.rolProviderDao 	= rolProviderDao;
		this.userProviderDao	= userProviderDao;
		this.providerPharmacyDao= providerPharmacyDao;
	}

	@Transactional(readOnly = true)	
	public List<Provider> getlistProviders(){
		return providerDao.getlistProviders();
	}
	@Transactional(readOnly = true)	
	public List<Provider> getlistProvidersDischarged(){
		return providerDao.getlistProvidersDischarged();
	}
	@Transactional(readOnly = true)	
	public List<Provider> getListProvider(boolean active){
		return providerDao.getListProvider(active);
	}
	@Transactional(readOnly = true)
    public List<Provider> getListProvider(Integer pharmacyId,boolean active){
    	List<Provider> listProvider = providerDao.getListProvider(active);
    	List<Providerpharmacy> listProviderPharmacy = providerPharmacyDao.getListProviderPharmacy(pharmacyId, active);
    	List<Provider> res = new ArrayList<Provider>();
    	for (Provider provider : listProvider) {
			for (Providerpharmacy providerpharmacy : listProviderPharmacy) {
				if(providerpharmacy.getProvider().getProviderId() == provider.getProviderId() && providerpharmacy.getPharmacy().getPharmacyId() == pharmacyId){
					res.add(provider);
				}
			}
		}
       	return res;
    }
	@Transactional(readOnly = true)	
	public Provider searchProviderByProviderId(Integer providerId){
		return providerDao.findProviderByProviderId(providerId);
	}
	@Transactional
	public void addProvider(Provider provider){
		providerDao.add(provider);
	}
	@Transactional
	public void updateProvider(Provider provider){
		providerDao.update(provider);
	}

	
	
	@Transactional(readOnly = true)	
	public List<Rolprovider> getListRolProvider(){
		return rolProviderDao.getListRolProvider();
	}
	@Transactional(readOnly = true)
    public Rolprovider searchRolProviderByRolProviderId(Integer rolProviderId){
    	return rolProviderDao.findByRolProviderId(rolProviderId);
    }
	
	
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProvider(){
		return userProviderDao.getListUserProvider();
	}
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProvider(boolean active){
		return userProviderDao.getListUserProvider(active);
	}
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProviderByRolProviderIdByActive(Integer rolProviderId,boolean active){
		return userProviderDao.getListUserProviderByRolProviderIdByActive(rolProviderId, active);
	}
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProviderByRolProviderIdByProviderIdByActive(Integer rolProviderId,Integer providerId,boolean active){
		return userProviderDao.getListUserProviderByRolProviderIdByProviderIdByActive(rolProviderId, providerId, active);
	}
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProviderByproviderId(Integer providerId){
		return userProviderDao.getListUserProviderByproviderId(providerId);
	}
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProviderByuserProviderId(Integer userProviderId){
		return userProviderDao.getListUserProviderByuserProviderId(userProviderId);
	}
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProviderByproviderId(Integer providerId,boolean active){
		return userProviderDao.getListUserProviderByproviderId(providerId, active);
	}
	@Transactional(readOnly = true)
	public List<Userprovider> getListUserProviderByuserProviderId(Integer userProviderId,boolean active){
		return userProviderDao.getListUserProviderByuserProviderId(userProviderId, active);
	}
	@Transactional(readOnly = true)
	public Userprovider searchUserProviderByUserProviderIdByProviderId(Integer userProviderId,Integer providerId){
		return userProviderDao.findByUserProviderIdByProviderId(userProviderId, providerId);
	}				
	@Transactional
    public void updateUserProvider(Userprovider userProvider){
    	userProviderDao.update(userProvider);
    }
	@Transactional
	public void addUserProvider(Userprovider userProvider){
		userProviderDao.add(userProvider);
	}
	
	@Transactional
   	public void removeUserProvider(Userprovider userProvider) {
		userProviderDao.remove(userProvider);
   	}
	
	@Transactional(readOnly = true)
	public Integer searchUserProviderIdByuserProviderIdMax(){
		return userProviderDao.findByuserProviderIdMax();
	}
	
}
